1
2
3
4
5
6
7 package ca.uhn.cache.util;
8
9 import java.util.Date;
10
11 import org.jmock.Mock;
12 import org.jmock.MockObjectTestCase;
13 import org.jmock.core.Constraint;
14
15 import ca.uhn.cache.IDataItem;
16 import ca.uhn.cache.IQuery;
17 import ca.uhn.cache.IQueryResult;
18 import ca.uhn.cache.ISemanticCache;
19 import ca.uhn.cache.VolatilityEnum;
20 import ca.uhn.cache.exception.CacheException;
21 import ca.uhn.cache.impl.DataItem;
22 import ca.uhn.cache.impl.Query;
23 import ca.uhn.cache.impl.QueryResult;
24
25 /***
26 * Test cases for QueryProcessor.
27 *
28 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
29 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:01 $ by $Author: bryan_tripp $
30 */
31 public class QueryProcessorTest extends MockObjectTestCase {
32
33 private Mock myCacheMock;
34 private ISemanticCache myCache;
35
36 /***
37 * @param theName The name of the test case.
38 */
39 public QueryProcessorTest(String theName) {
40 super(theName);
41 }
42
43 /***
44 * {@inheritDoc}
45 */
46 protected void setUp() throws Exception {
47 super.setUp();
48
49 myCacheMock = mock( ISemanticCache.class );
50 myCache = (ISemanticCache) myCacheMock.proxy();
51 }
52
53
54 /***
55 * @throws InterruptedException ...
56 * @throws CacheException ...
57 */
58 public void testDeclareException() throws CacheException, InterruptedException {
59 IQuery dummyQuery = new Query();
60 IQueryResult dummyResult = new QueryResult();
61
62 myCacheMock.expects( once() ).method( "remainder" ).will(returnValue(new IQuery[] {dummyQuery}));
63 myCacheMock.expects( once() ).method( "get" ).will( returnValue(dummyResult));
64
65 QueryProcessor processor = new QueryProcessor(dummyQuery, myCache, 3, null);
66
67
68 String msg = "test";
69 processor.declareException(msg, new Exception());
70
71 Thread.sleep(100);
72
73 try {
74 processor.getCombinedResult();
75 fail("Should have thrown declared exception");
76 } catch (CacheException e) {
77 assertEquals(msg, e.getMessage());
78 }
79 }
80
81 /***
82 * @throws InterruptedException ...
83 * @throws CacheException ...
84 */
85 public void testBasicFlow() throws CacheException, InterruptedException {
86 IQuery dummyQuery = new Query();
87 IQueryResult dummyResult = new QueryResult();
88 Date date = new Date();
89 dummyResult.add(new DataItem(new String("a"), new String("a"), dummyQuery, VolatilityEnum.STABLE, date));
90
91 myCacheMock.expects( once() ).method( "remainder" ).will(returnValue(new IQuery[] {dummyQuery}));
92 myCacheMock.expects( once() ).method( "get" ).will( returnValue(dummyResult));
93
94 QueryProcessor processor = new QueryProcessor(dummyQuery, myCache, 3, null);
95
96 assertEquals(1, processor.getCombinedResult().size());
97 assertEquals("a", ((IDataItem) processor.getCombinedResult().iterator().next()).getValue());
98
99 IQueryResult dummySourceResult = new QueryResult();
100 dummySourceResult.add(new DataItem(new String("b"), new String("b"), dummyQuery, VolatilityEnum.STABLE, date));
101
102 myCacheMock.expects( once() ).method( "put" )
103 .with(new Constraint[] {eq(dummyQuery), eq(dummySourceResult)});
104 processor.setSourceResult(dummyQuery, dummySourceResult);
105
106
107 Thread.sleep(100);
108
109 assertEquals(2, processor.getCombinedResult().size());
110 }
111 }